<template>
  <div class="upload">
    <el-upload
      class="avatar-uploader"
      :action="$config.uploadUrl"
      :show-file-list="false"
      :headers="{ Authorization: token }"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>
export default {
  props: {
    imgUrl: {
      type: String,
      default: "",
    },
    checkStatus: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      imageUrl: "",
      token:sessionStorage.getItem('token') ,
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      console.log(this.token)
      console.log(res)
      this.imageUrl = URL.createObjectURL(file.raw);
      res.errCode === 10000
        ? this.$message.success("上传成功")
        : this.$message.success("上传失败");
    },
    beforeAvatarUpload(file) {
      this.$emit("uploadCheck", file);
      return this.checkStatus;
    },
  },
  watch: {
    imgUrl(newV) {
      this.imageUrl = newV;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>